home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7845 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 28 Feb 1996 15:59:36 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb28085936@qcd.lanl.gov>
  8. References: <177399702S86.JW1675A@american.edu> <4gt9i7INN76i@keats.ugrad.cs.ubc.ca>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: c2a192@ugrad.cs.ubc.ca's message of 26 Feb 1996 13:43:35 -0800
  13.  
  14. In article <4gt9i7INN76i@keats.ugrad.cs.ubc.ca>
  15. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  16. <snip>
  17. KK:  >I'm doing some file manipulation and between steps A and B, I
  18. need to sort. 
  19. KK:  >For various reasons, I don't want to popen() to the sort
  20. utility--I want to 
  21. KK:  >use qsort.  Here's what I'm doing:
  22. KK:  > 
  23. KK:  >   get number of lines in the file
  24. KK:  >   (char**)malloc with enough room for all lines
  25. KK:  >   for each line in the file {
  26. KK:  >     (char*)malloc(90)  /* 90 is enough room for each line in file */
  27. KK:  >     copy each line into the newly malloc()ed space
  28. KK:  >     point a (char**) to the newly malloc()ed space
  29. KK:  >   }
  30. KK:  > 
  31. KK:  >so now I should have "lines" number of pointers to pointers to char,
  32. KK:  >each one pointing to 90 bytes containing a line in the file.
  33. KK:  > 
  34. KK:  >So, I call qsort(array[0], lines, 90, compare)
  35. KK:  >where compare is my comparison function -- prepared as discussed
  36. KK:  >in the FAQ.  Now all I get are core dumps during the call to qsort().
  37. KK:  >:-)
  38. KK: 
  39. KK: You need a pointer to the first element of the array, not the first element
  40. KK: itself. You should be calling it qsort(array, ... );
  41. KK: 
  42.  
  43. This is the second wrong answer, though I don't blame them. People who
  44. do not post code deserve this. The design, I believe, is fundamentally
  45. flawed and such patches of removing [0] will not solve
  46. it. 
  47.  
  48. Look at the 90 in the call to malloc. Now look at the statement above
  49. about `(char**)malloc'. Try to mentally complete that to compilable
  50. code ... does something strike someone as unusual :-)? 
  51.  
  52. Cheers
  53. Tanmoy
  54. --
  55. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  56. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  57. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  58. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  59. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  60. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  61.